$(document).ready(function(){
	restoreEventScreen();
});

function restoreEventScreen(){
	$( "#date" ).datepicker({
		showOn: "button",
		buttonImage: "images/layout/calendar.png",
		buttonImageOnly: true,
		changeMonth: true,
		changeYear: true
	});
	
	$("#location-state").autocomplete({
		source: function( request, response ) {
			$.get("/dotcao/ws/states/list/"+$("#location-state").val(), function(data) {
				response($.map(data, function(item) {
					return {
							label: item.acronym + " - " + item.name,
							value: item.acronym
					};
				}));
			});
		},
		
		minLength: 2,
		select: function( event, ui ) {
			alert("ainda não faz nada.... mas vai fazer...");
		},
		
		open: function() {
			$( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
		},
		
		close: function() {
			$( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
		}
	});
	
	$("#save").button();
	$("#save").bind("click", function(){
		var user = {
				username: $("#username").val(),
				password: $("#password").val(),
				firstname: $("#firstname").val(),
				lastname: $("#lastname").val(),
				birthday: $("#birthday").val()
		};
		
		$.ajax({
			type: 'POST',
			url: "/dotcao/ws/events/save",
			data: $.toJSON(user),
			contentType: 'application/json',
			dataType: 'json',
			async: true,
			success: function (data) {
				$("<div />").html(data.content).appendTo("#content").dialog({
					title: "Cadastro",
					show: "fade",
					hide: "fade",
					width: 300,
					height: 150,
					modal: true,
					buttons: {
						OK: function(){
							$(this).dialog("close");
						}
					}
				});
				
			}
		});
	});
	
	$("#cancel").button();
};